home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / mac / PageMaker 6.5 SDK Mac / SourceCode / UtilityCode / PageMakerUtils.cpp < prev   
C/C++ Source or Header  |  1996-09-04  |  13KB  |  406 lines

  1. /*
  2.  *--- PageMaker Utils.c --------------------------------------------------
  3.  * Copyright (c) 1995-1996 Adobe Systems, Inc.  All rights reserved.
  4.  *
  5.  * This collection of functions implements the routines to make callbacks
  6.  * to PageMaker and routines to pack and unpack the buffers used in
  7.  * callbacks.  Most of these functions were formerly implemented as
  8.  * macros, but have been converted into function for space efficiency
  9.  * and compile time type-checking safety.
  10.  *
  11.  * Macintosh only - PageMaker does not preserve the current window/dialog 
  12.  * Port when responding to a command or query, so these callbacks do so by
  13.  * using the GetPort()/SetPort() calls.
  14.  *------------------------------------------------------------------------
  15.  */
  16.  
  17. #include "PMPlugin.h"
  18.  
  19. #ifdef WINDOWS
  20.     #include <Windows.h>
  21. #endif //WINDOWS
  22.  
  23. #include "PMUtils.h"
  24.  
  25. #define CallPageMaker(thePB) thePB->pmCallback(thePB)
  26.  
  27. #ifdef WINDOWS
  28.     PMHandle    hDllInstance;
  29. #endif //WINDOWS
  30.  
  31. static void ClearRequestBlock(sPMParamBlockPtr thePB);
  32. static void ClearReplyBlock(sPMParamBlockPtr thePB);
  33.  
  34. #ifdef WINDOWS
  35.     PMBool WINAPI DllMain(HINSTANCE hInstance, ULONG ul_reason, LPVOID lpvReserved);
  36. #endif
  37.  
  38. //#pragma optimize("",off) // Plug-ins currently crash if the PBBin functions are optimized.
  39. /*
  40.  *--- PBBinCommand -------------------------------------------------------
  41.  * Issue a binary command to PageMaker.  The binary command parameters
  42.  * should be in the array pointed to by 'theRequest'.  You should use
  43.  * the LPPut<xxx> functions to build the binary command information.
  44.  *------------------------------------------------------------------------
  45.  */
  46. PMErr PBBinCommand(sPMParamBlockPtr thePB, ePMCommand op, ePMRefStyle theStyle, void * theRequest, unsigned long theRequestSize)
  47. {
  48.     thePB->opCode = op;
  49.     thePB->requestData = (PMHandle) theRequest;
  50.     thePB->requestSize = theRequestSize;
  51.     thePB->requestStyle = theStyle;
  52.     
  53.     ClearReplyBlock(thePB);
  54.     return CallPageMaker(thePB);
  55. }
  56.  
  57. /*
  58.  *--- PBBinCommandByShortValue -------------------------------------------
  59.  * Issue a command that requires a single short integer parameter (for
  60.  * example, the Alignment command).
  61.  *
  62.  * The #ifdef statments are required because on the Macintosh platforms
  63.  * a simple assignment of the short value to requestData puts the short value 
  64.  * in the wrong 16 bits - so the bitwise shift operator *<<* is used as the 
  65.  * remedy. On the Windows platforms a simple assignment is all that is needed. 
  66.  *------------------------------------------------------------------------
  67.  */
  68. PMErr PBBinCommandByShortValue(sPMParamBlockPtr thePB, ePMCommand op, unsigned short v)
  69. {
  70.     thePB->opCode = op;
  71.     thePB->requestData = &v;
  72.     thePB->requestSize = sizeof(short*);
  73.     thePB->requestStyle = kXRSPointer;
  74.     
  75.     ClearReplyBlock(thePB);
  76.     return CallPageMaker(thePB);
  77. }
  78. /*
  79.  *--- PBBinCommandByLongValue --------------------------------------------
  80.  * Issue a command that requires a single long integer parameter (for
  81.  * example, the SelectID command).
  82.  *------------------------------------------------------------------------
  83.  */
  84. PMErr PBBinCommandByLongValue(sPMParamBlockPtr thePB, ePMCommand op, unsigned long v)
  85. {
  86.     thePB->opCode = op;
  87.     thePB->requestData  = (void *)v;
  88.     thePB->requestSize  = sizeof(unsigned long);
  89.     thePB->requestStyle = kXRSValue;
  90.  
  91.     ClearReplyBlock(thePB);
  92.     return CallPageMaker(thePB);
  93.  
  94. }
  95.  
  96. /*
  97.  *--- PBBinQuery ---------------------------------------------------------
  98.  * Issue a binary query to PageMaker.  The binary data will be returned
  99.  * in the array 'r', which the caller must provide.  You can use the
  100.  * LPGet<xxx> functions to extract fields from the returned data.
  101.  *
  102.  * For queries that don't return string information, you can define
  103.  * a C structure to directly access the returned information.  See
  104.  * StoryInfo.c for an example of this.
  105.  *------------------------------------------------------------------------
  106.  */
  107. PMErr PBBinQuery(sPMParamBlockPtr thePB, ePMQuery op, ePMRefStyle theStyle, void * r, unsigned long rsz)
  108. {
  109.     thePB->opCode = op;
  110.     ClearRequestBlock(thePB);
  111.     thePB->replyStyle = theStyle;
  112.     thePB->replyData = (PMHandle) r;
  113.     thePB->replySize = rsz;
  114.         
  115.     return CallPageMaker(thePB);
  116. }
  117.  
  118. /*
  119.  *--- PBBinQueryWithParms ------------------------------------------------
  120.  * Issue a binary query that requires parameters (for example,
  121.  * GetColorInfo).  
  122.  *------------------------------------------------------------------------
  123.  */
  124. PMErr PBBinQueryWithParms(sPMParamBlockPtr thePB, ePMQuery op, 
  125.     ePMRefStyle theRequestStyle, void * theRequestData, unsigned long theRequestSize,
  126.     ePMRefStyle theReplyStyle,   void * theReplyData,   unsigned long theReplySize)
  127. {
  128.     thePB->opCode = op;
  129.     
  130.     thePB->requestStyle    = theRequestStyle;
  131.     thePB->requestData    = (PMHandle) theRequestData;
  132.     thePB->requestSize    = theRequestSize;
  133.  
  134.     thePB->replyStyle    = theReplyStyle;
  135.     thePB->replyData    = (PMHandle) theReplyData;
  136.     thePB->replySize    = theReplySize;
  137.  
  138.     return CallPageMaker(thePB);
  139. }
  140.  
  141. /*
  142.  *--- PBTextCommand ------------------------------------------------------
  143.  * Issue a text command to PageMaker.
  144.  * Generally, you should use PBBinCommand, as binary commands are
  145.  * more efficient.
  146.  *------------------------------------------------------------------------
  147.  */
  148.  
  149. PMErr PBTextCommand(sPMParamBlockPtr thePB, ePMRefStyle theStyle, char * theText)
  150. {
  151.     thePB->opCode = pm_execute_text;
  152.     thePB->requestData = (PMHandle) theText;
  153.     thePB->requestSize = strlen(theText) + 1;
  154.     thePB->requestStyle = theStyle;
  155.     ClearReplyBlock(thePB);
  156.  
  157.     return CallPageMaker(thePB);
  158. }
  159.  
  160. /*
  161.  *--- PBTextQuery --------------------------------------------------------
  162.  * Issue a text query to PageMaker.
  163.  * Generally you should use PBBinQuery, as binary queries are
  164.  * more efficient.
  165.  *------------------------------------------------------------------------
  166.  */
  167.  
  168. PMErr PBTextQuery(sPMParamBlockPtr thePB, char * theText, ePMRefStyle theStyle, char * theReplyBuf, unsigned long replyLen, ePMRefStyle theReplyStyle)
  169. {
  170.     thePB->opCode = pm_evaluate_text;
  171.     thePB->requestData  = (PMHandle) theText;
  172.     thePB->requestSize  = strlen(theText) + 1;
  173.     thePB->requestStyle = theStyle;
  174.     
  175.     thePB->replyData  = (PMHandle) theReplyBuf;
  176.     thePB->replySize  = replyLen;
  177.     thePB->replyStyle = theReplyStyle;
  178.  
  179.     return CallPageMaker(thePB);
  180. }
  181.  
  182. //#pragma optimize("",on)
  183. /*
  184.  *--- ClearRequestBlock/ClearReplyBlock ----------------------------------
  185.  * Local functions to clear the sub-structures of the parameter block.
  186.  *------------------------------------------------------------------------
  187.  */
  188.  
  189. void ClearRequestBlock(sPMParamBlockPtr thePB)
  190. {
  191.     thePB->requestData     = (PMHandle) NULL;
  192.     thePB->requestSize  = 0;
  193.     thePB->requestStyle = kXRSNull;
  194.     thePB->requestUnits    = 0;
  195. }
  196.  
  197. void ClearReplyBlock(sPMParamBlockPtr thePB)
  198. {
  199.     thePB->replyData  = (PMHandle) NULL;
  200.     thePB->replySize  = 0;
  201.     thePB->replyStyle = kXRSNull;
  202.     thePB->replyUnits = 0;
  203. }
  204.  
  205. /*
  206.  *--- LPGetString --------------------------------------------------------
  207.  *  Extracts a character string from the data reference given.
  208.  *
  209.  *     pDst    -       pointer to destination string.
  210.  *     pSrc    -       pointer to source data block.
  211.  *     maxlen  -       max length of string to copy.
  212.  *
  213.  * return value is the number of copiable bytes plus any
  214.  * null-terminating or pad bytes (pad to make an even byte boundary.)
  215.  *------------------------------------------------------------------------
  216.  */
  217. size_t LPGetString(char * pDst, const void * pSrc, short maxlen)
  218. {
  219.     size_t tLen;
  220.     tLen = strlen((char *)pSrc) + 1;
  221.     if (tLen > maxlen)
  222.         tLen = maxlen;
  223.     memcpy(pDst, pSrc, tLen);
  224.  
  225.     if (tLen & 0x01)
  226.         ++tLen;
  227.     return tLen;
  228. }
  229.  
  230. /*
  231.  *--- LPPutString --------------------------------------------------------
  232.  * Inserts the source string into the destination data block.
  233.  *
  234.  *    pDst    -       pointer to destination data block.
  235.  *    pSrc    -       pointer to source string.
  236.  *    maxlen  -       max length of string to copy.
  237.  *
  238.  * return value is the number of copiable bytes plus any null-terminating
  239.  * or pad bytes (pad to make an even byte boundary.)
  240.  *------------------------------------------------------------------------
  241.  */
  242. size_t LPPutString(void * pDst, const char * pSrc)
  243. {
  244.     size_t tLen;
  245.  
  246.     tLen = strlen(pSrc) + 1;
  247.     memcpy(pDst, pSrc, tLen);
  248.  
  249.     if (tLen & 0x01)
  250.         ++tLen;
  251.     return tLen;
  252. }
  253.  
  254.  
  255. /*
  256.  *--- LPGetBuffer --------------------------------------------------------
  257.  * Extracts a buffer from the data reference given.
  258.  *
  259.  *    pDst    -       pointer to destination buffer.
  260.  *    pSrc    -       pointer to source data block.
  261.  *    len     -       number of bytes to transfer.
  262.  *
  263.  * return value is the number of bytes transfered plus
  264.  * any pad bytes (pad to make an even byte boundary.)
  265.  *------------------------------------------------------------------------
  266.  */
  267. size_t LPGetBuffer(char * pDst, const void * pSrc, short len)
  268. {
  269.     register size_t tLen = len;
  270.     memcpy(pDst, pSrc, tLen);
  271.  
  272.     if (tLen & 0x01)
  273.         ++tLen;
  274.     return tLen;
  275. }
  276.  
  277. /*
  278.  *--- LPPutBuffer --------------------------------------------------------
  279.  * Inserts the source buffer into the destination data block.
  280.  *
  281.  *    pDst    -       pointer to destination data block.
  282.  *    pSrc    -       pointer to source buffer.
  283.  *    len     -       number of bytes to transfer.
  284.  *
  285.  * return value is the number of bytes transfered plus
  286.  * any pad bytes (pad to make an even byte boundary.)
  287.  *------------------------------------------------------------------------
  288.  */
  289. size_t LPPutBuffer(void * pDst, const char * pSrc, short maxlen)
  290. {
  291.     register size_t tLen = maxlen;
  292.     memcpy(pDst, pSrc, tLen);
  293.  
  294.     if (tLen & 0x01)
  295.         ++tLen;
  296.     return tLen;
  297. }
  298.  
  299. /*
  300.  *--- LPPutShort/Long/Handle and LPGetShort/Long/Handle -----------------
  301.  *These are functions that replace the macros by the same names.
  302.  * You should use the functions rather than the macros because
  303.  * they are not subject to unwanted side effects and are typesafe.
  304.  *-----------------------------------------------------------------------
  305.  */
  306. size_t LPPutShort(void * pDst, short pSrc)
  307. {
  308.     *(short *) pDst = pSrc;
  309.     return sizeof(short);
  310. }
  311.  
  312. size_t LPPutLong(void * pDst, long pSrc)
  313. {
  314.     *(long *) pDst = pSrc;
  315.     return sizeof(long);
  316. }
  317.  
  318. size_t LPPutULong(void * pDst, unsigned long pSrc)
  319. {
  320.     *(unsigned long *) pDst = pSrc;
  321.     return sizeof(unsigned long);
  322. }
  323.  
  324. size_t LPPutHandle(void * pDst, PMHandle pSrc)
  325. {
  326.     *(PMHandle *) pDst = pSrc;
  327.     return sizeof(PMHandle);
  328. }
  329.  
  330. size_t LPGetShort(short * pDst, const void * pSrc)
  331. {
  332.     *pDst  = * (short *) pSrc;
  333.     return sizeof(short);
  334. }
  335.  
  336. size_t LPGetLong(long * pDst, const void * pSrc)
  337. {
  338.     *pDst  = * (long *) pSrc;
  339.     return sizeof(long);
  340. }
  341.  
  342. size_t LPGetULong(unsigned long * pDst, const void * pSrc)
  343. {
  344.     *pDst  = * (unsigned long *) pSrc;
  345.     return sizeof(unsigned long);
  346. }
  347.  
  348. size_t LPGetHandle(PMHandle * pDst, const void * pSrc)
  349. {
  350.     *pDst  = * (PMHandle *) pSrc;
  351.     return sizeof(PMHandle);
  352. }
  353.  
  354.  
  355. /*
  356.  *--- BuildErrorMessages ---------------------------------------
  357.  *    This is a utility routine that takes a string parameter,
  358.  *    allocates a handle for it, and stuffs it in the 
  359.  *    errMessage field of the parameter block.
  360.  *
  361.  *    Note:   The handle becomes property of PageMaker, which
  362.  *            will free it after it's displayed.
  363.  *--------------------------------------------------------------
  364.  */
  365. void BuildErrorMessages(sPMParamBlockPtr thePB, char * errText, char * cantText)
  366. {
  367.     PMHandle    h;
  368.     char     *ch;
  369.     short    lenErrText  = strlen(errText);
  370.     short    lenCantText = strlen(cantText);
  371.  
  372.     //---------------------------------------------------------
  373.     //    PM requires that its strings be padded to even-byte
  374.     //    boundaries.    This means that the length as reported by
  375.     //    strlen() must be odd so that when the NULL terminator
  376.     //    is added the length becomes even.
  377.     //---------------------------------------------------------
  378.     if (! (lenErrText  & 0x01) ) ++lenErrText;
  379.     if (! (lenCantText & 0x01) ) ++lenCantText;
  380.     
  381.     h   = MMAlloc(lenErrText + lenCantText + 3);    // 3 NULL terminators.  Final one signifies end.
  382.     ch  = (char *) MMLock(h);
  383.     ch += LPPutString(ch, errText);
  384.     
  385.     //---------------------------------------------------------
  386.     //    The "Can't" message is optional.
  387.     //    If it's present, PM does not display it's default
  388.     //    Cant message, so if we don't have one we must append
  389.     //    the final NULL to the end of the previous string.
  390.     //---------------------------------------------------------
  391.     if (*cantText)
  392.     {
  393.         ch += LPPutString(ch, cantText);
  394.     }
  395.     
  396.     *ch = 0x00;                                // add the final NULL terminator.
  397.     
  398.     MMUnlock(h);
  399.     
  400.     thePB->errMessage = h;
  401. }
  402.  
  403.  
  404.  
  405.  
  406.